
package com.gw.business.statistics.exception.controller;

import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gw.business.analysis.model.UserTrackDataDetail;
import com.gw.business.common.util.DateUtil;
import com.gw.business.common.util.excel.ConditionCollection;
import com.gw.business.common.util.excel.ExcelOperate;
import com.gw.business.statistics.detailed.model.DetailForm;
import com.gw.platform.controller.DefaultPageController;
import com.gw.platform.page.impl.Page;
import com.gw.platform.util.DateUtils;
import com.gw.platform.util.StringUtils;


/**
 * 异常实现controller
 * @author  kidy
 * @since   1.0
 * @version 2015年1月29日 kidy
 */
@Scope("prototype")
@Controller
@RequestMapping("exception")
public class ExceptionController extends DefaultPageController {
    private ExcelOperate excelOperate;
    /**
     * 查询异常数据
     * @return
     */
    @RequestMapping("/page")
    public String queryException(){
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("numPerPage");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String userId = request.getParameter("userId");
        String ip = request.getParameter("ip");
        String exceptionBeginTime =(String) request.getParameter("exceptionBeginTime");
        String exceptionEndTime =(String) request.getParameter("exceptionEndTime");
        String exceptionTemp = (String)request.getParameter("exceptionTemp");
        if(StringUtils.isEmpty(pageNum)){
            pageNum = "1";
            pageSize = Page.DEFAULT_PAGE_SIZE + "";
        }
        if(null==exceptionTemp){
            exceptionBeginTime=DateUtil.getDateFormat((DateUtil.addHours(new Date(), -1)), "yyyy-MM-dd HH:mm:ss");
            exceptionEndTime=DateUtil.getDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss");
            startTime=exceptionBeginTime;
            endTime=exceptionEndTime;
        }
     // 设置当前页
        setCurrent(Long.parseLong(pageNum));
        Map<String, Object> map = new HashMap<String, Object>();
        if (startTime!=null&&startTime!="") {
            map.put("startTime", startTime);
          
        }  
        if (endTime!=null&&endTime!="") {
            map.put("endTime", endTime);
          
        }  
        if (userId!=null&&userId!="") {
            map.put("userId", userId);
          
        }  
        if (ip!=null&&ip!="") {
            map.put("ip", ip);
          
        }  
        // 查询异常列表
        page = defaultService.page("queryExceptionData", "queryExceptionDataCount", getCurrent(), map, Integer.parseInt(pageSize));
        request.setAttribute("page", page);
        request.setAttribute("startTime", startTime);
        request.setAttribute("endTime", endTime);
        request.setAttribute("exceptionBeginTime",exceptionBeginTime);
        request.setAttribute("exceptionEndTime", exceptionEndTime);
        request.setAttribute("userId", userId);
        request.setAttribute("ip", ip);
        return "exception/exceptionPage";
    }

    /**
     * 导出
     * 
     * @param req
     * @param response
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @RequestMapping("/export")
    public void export(HttpServletRequest req, HttpServletResponse response, DetailForm detailForm) {
        OutputStream out = null;
        try {
            String fileName = "点击异常";
            String fileName2 = new String(fileName.getBytes("gb2312"), "iso-8859-1");
            // 文件名
            response.setContentType("application/vnd.ms-excel");// 定义输出类型
            response.setHeader("content-disposition", "attachment;filename=" + fileName2 + ".xls");
            String pageNum = request.getParameter("pageNum");
            String pageSize = request.getParameter("numPerPage");
            String startTime = request.getParameter("startTime");
            String endTime = request.getParameter("endTime");
            String userId = request.getParameter("userId");
            String ip = request.getParameter("ip");
            Map<String, Object> map = new HashMap<String, Object>();
            if(StringUtils.isEmpty(pageNum)){
                pageNum = "1";
                pageSize = Page.DEFAULT_PAGE_SIZE + "";
            }
         // 设置当前页
            setCurrent(Long.parseLong(pageNum));
            if (startTime!=null&&startTime!="") {
                map.put("startTime",  DateUtils.stringToDate(startTime,"yyyy-MM-dd HH:mm"));
              
            }  
            if (endTime!=null&&endTime!="") {
                map.put("endTime",  DateUtils.stringToDate(endTime,"yyyy-MM-dd HH:mm"));
              
            }  
            if (userId!=null&&userId!="") {
                map.put("userId", userId);
              
            }  
            if (ip!=null&&ip!="") {
                map.put("ip", ip);
              
            }  
            // 查询异常列表
            page = defaultService.page("queryExceptionData", "queryExceptionDataCount", getCurrent(), map, Integer.parseInt(pageSize));
            
            excelOperate = new ExcelOperate();
            excelOperate.setDataList((List<UserTrackDataDetail>) page.getData());
            excelOperate.setThAndFieldMap(ConditionCollection.getCommuExceptionTHFieldMap("1"));
            excelOperate.excelExportTemp(out, response);
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.flush();
                out.close();
            } catch (Exception e2) {
                
            }
        }
        
    }
    
    /**
     *  异常详情页面
     * @return
     */
    @RequestMapping("/detail")
   public String exceptionDetail(){
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("numPerPage");
        String time = request.getParameter("time");
      
        if(StringUtils.isEmpty(pageNum)){
            pageNum = "1";
            pageSize = Page.DEFAULT_PAGE_SIZE + "";
        }
     // 设置当前页
        setCurrent(Long.parseLong(pageNum));
        Map<String, Object> map = new HashMap<String, Object>();
        if (time!=null&&time!="") {
            map.put("time", time);
        }  
        
        page = defaultService.page("queryExceptionUrlData", "queryExceptionUrlDataCount", getCurrent(), map, Integer.parseInt(pageSize));
        request.setAttribute("page", page);
        request.setAttribute("time", time);
       
        return "exception/detail";
   }
    
    /**
     * 导出
     * 
     * @param req
     * @param response
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @RequestMapping("/detailExport")
    public void detailExport(HttpServletRequest req, HttpServletResponse response, DetailForm detailForm) {
        OutputStream out = null;
        try {
            String pageNum = request.getParameter("pageNum");
            String pageSize = request.getParameter("numPerPage");
            String time = request.getParameter("time");
            String fileName ="详细列表";
            String fileName2 = new String(fileName.getBytes("gb2312"), "iso-8859-1");
            // 文件名
            response.setContentType("application/vnd.ms-excel");// 定义输出类型
            response.setHeader("content-disposition", "attachment;filename=" + fileName2 + ".xls");
            
       
            if(StringUtils.isEmpty(pageNum)){
                pageNum = "1";
                pageSize = Page.DEFAULT_PAGE_SIZE + "";
            }
         // 设置当前页
            setCurrent(Long.parseLong(pageNum));
            Map<String, Object> map = new HashMap<String, Object>();
            if (time!=null&&time!="") {
                map.put("time",time);
            } 
            // 查询用户列表
            page = defaultService.page("queryExceptionUrlData", "queryExceptionUrlDataCount", getCurrent(), map, Integer.parseInt(pageSize));
            excelOperate = new ExcelOperate();
            excelOperate.setDataList((List<UserTrackDataDetail>) page.getData());
            excelOperate.setThAndFieldMap(ConditionCollection.getCommuExceptionTHFieldMap("2"));
            excelOperate.excelExportTemp(out, response);
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.flush();
                out.close();
            } catch (Exception e2) {
                
            }
        }
        
    }
}
